package cn.itcast.store.service.serviceImp;

import java.sql.SQLException;
import java.util.List;

import cn.itcast.store.dao.ProductDao;
import cn.itcast.store.dao.daoImp.ProductDaoImp;
import cn.itcast.store.domain.PageModel;
import cn.itcast.store.domain.Product;
import cn.itcast.store.service.ProductService;

public class ProductServiceImp implements ProductService{
	ProductDao ProductDao =new ProductDaoImp();
	@Override
	public List<Product> findNewProducts() throws SQLException {
		return ProductDao.findNewProducts();
	}

	@Override
	public List<Product> findHotProducts() throws SQLException {
		return ProductDao.findHotProducts();
	}

	@Override
	public Product findProductByPid(String pid) throws SQLException {
		return ProductDao.findProductByPid(pid);
	}

	@Override
	public PageModel findProductsWithCidAndPage(String cid, int curNum) throws SQLException {
		//1_创建PageModel对象,目的:携带分页参数 select count(*) from product where cid=?
		int totalRecords=ProductDao.findTotalNum(cid);
		
		PageModel pm=new PageModel(curNum, totalRecords, 12);
		
		//2_关联集合 SELECT * FROM product WHERE cid=1 LIMIT (当前页-1)*5,5;
		List<Product> list=ProductDao.findProductsWithCidAndPage(cid,pm.getStartIndex(),pm.getPageSize());
		//System.out.println("数据库中有这种商品  "+list);
		pm.setList(list);
		//3_关联url
		pm.setUrl("ProductServlet?method=findProductsWithCidAndPage&cid="+cid);
		return pm;
	}

	@Override
	public PageModel findAllProductsWithPage(Integer curNum) throws SQLException {
		//获取总记录数
		int totalRecords=ProductDao.finProductTotalRecords();
		//创建PageModel对象
		PageModel pm=new PageModel(curNum, totalRecords, 10);
		//获取Product的list集合 SELECT * FROM product  LIMIT (当前页-1)*5,5;
		List<Product> list = ProductDao.findProductWithPage(pm.getStartIndex(),pm.getPageSize());
		//PageModel关联list集合
		//返回PageModel
		pm.setList(list);
		
		
		return pm;
	}

	@Override
	public PageModel findProductsWithPageAndPflag(Integer curNum) throws Exception {
		//获取总记录数
				int totalRecords=ProductDao.findPflagProductTotalRecords();
				//创建PageModel对象
				PageModel pm=new PageModel(curNum, totalRecords, 10);
				//获取Product的list集合 SELECT * FROM product  LIMIT (当前页-1)*5,5;
				List<Product> list = ProductDao.findFlagProductWithPage(pm.getStartIndex(),pm.getPageSize());
				//PageModel关联list集合
				//返回PageModel
				pm.setList(list);
				
				
				return pm;
	}

	@Override
	public void deleteProductsByPid(String pid, int flag) {
		
		ProductDao.deleteProductsByPid(pid,flag);
	}

	@Override
	public void saveProduct(Product product) throws Exception {
		// TODO Auto-generated method stub
		ProductDao.saveProduct(product);
	}

	@Override
	public void editProduct(Product product) {
		// TODO Auto-generated method stub
		ProductDao.editProduct(product);
	}

	


}
